package com.his.admin.controller;

import com.his.admin.service.HospitalService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/hospital")
@AllArgsConstructor
@Api(value = "医院模块")
public class HospitalController {
    private HospitalService hospitalService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取医院列表",notes = "获取医院列表")
    public RestResult all(@RequestBody ListDto listDto) {
        return hospitalService.all(listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all/id")
    @ApiOperation(value = "获取医院id列表",notes = "获取医院id列表")
    public RestResult allId(@RequestBody Hospital hospital) {
        return hospitalService.allId(hospital);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加医院",notes = "增加医院")
    public RestResult add(@RequestBody Hospital hospital) {
        hospital.setCreateTime(LocalDateTime.now());
        if (hospitalService.save(hospital)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/update")
    @ApiOperation(value = "更新医院",notes = "更新医院")
    public RestResult update(@RequestBody Hospital hospital) {
        hospital.setUpdateTime(LocalDateTime.now());
        if (hospitalService.updateById(hospital)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除医院",notes = "删除医院")
    public RestResult delete(@RequestBody List<Integer> ids) {
        if (hospitalService.removeByIds(ids)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}